package com.cn.zsc.config;

import com.mysql.cj.jdbc.MysqlDataSource;
import com.mysql.cj.jdbc.MysqlXADataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.jta.JtaTransactionManager;
import javax.sql.DataSource;
import java.io.IOException;

@Configuration
@MapperScan(value = "com.cn.zsc.db108.dao", sqlSessionFactoryRef = "sqlSessionFactoryBean108")
public class ConfigDb108 {

//    @Bean("db108")
//    public DataSource db108(){
//        MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
//        mysqlXADataSource.setUser("zhousc");
//        mysqlXADataSource.setPassword("zhousc@123456");
//        mysqlXADataSource.setUrl("jdbc:mysql://192.168.56.108:3306/XA-108?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false");
//        // 需要用AtomikosDataSource数据源管理
//        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
//        atomikosDataSourceBean.setXaDataSource(mysqlXADataSource);
//        return atomikosDataSourceBean;
//    }

    @Bean("db108")
    public DataSource db108(){
        MysqlDataSource mysqlDataSource = new MysqlDataSource();
        mysqlDataSource.setUser("zhousc");
        mysqlDataSource.setPassword("zhousc@123456");
        mysqlDataSource.setUrl("jdbc:mysql://192.168.56.108:3306/XA-108?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false");
        return mysqlDataSource;
    }




    @Bean("sqlSessionFactoryBean108")
    public SqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("db108") DataSource dataSource) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("mybatis/db108/*.xml"));

        return sqlSessionFactoryBean;
    }



    /**
     * 配置XA事务管理器
     * @return
     */
//    @Bean("xaTransaction")
//    public JtaTransactionManager jtaTransactionManager(){
//        UserTransaction userTransaction = new UserTransactionImp();
//        UserTransactionManager userTransactionManager = new UserTransactionManager();
//        return new JtaTransactionManager(userTransaction,userTransactionManager);
//    }

    @Bean("tm108")
    public PlatformTransactionManager transactionManager(@Qualifier("db108") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }


}
